package com.dd.mall.protal.config;

import com.dd.common.entity.view.CommonResult;
import com.dd.common.error.ApiError;
import com.dd.common.util.ResultCode;
import com.google.common.base.Predicates;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 *
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * 函数名,可以随意命名,喜欢什么命名就什么命名
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("大盗商城前端APIs")
                .useDefaultResponseMessages(true)
                .genericModelSubstitutes(CommonResult.class)
                .globalResponseMessage(RequestMethod.GET,responseStatus())
                .globalResponseMessage(RequestMethod.POST,responseStatus())
                .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .select()
                /**控制暴露出去的路径下的实例
                 *
                1. 如果某个接口不想暴露,可以使用以下注解
                @ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                2. 默认swagger只会扫描同级目录，如果要知道扫描的目录可使用
                RequestHandlerSelectors.basePackage() 设置扫描目录

                 */
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
//                .pathMapping("/")
                ;
    }
    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("大盗商城")
                //条款地址
                .termsOfServiceUrl("http://despairyoke.github.io/")
                //创建人
                .contact(new Contact("zhhy","","zhhy"))
                .version("1.0")
                //描述
                .description("大盗商城前端接口")
                .build();
    }


    private List<ResponseMessage> responseStatus(){
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(ResultCode.values()).forEach(statusEnums -> {
            responseMessageList.add(
                    new ResponseMessageBuilder().code(statusEnums.getCode()).message(statusEnums.getMessage())
//                            .responseModel(new ModelRef("ApiError"))
                            .build()
            );
        });
        return responseMessageList;
    }
}
